/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actor.weapons;

import Actor.Actor;
import Actor.Projectile;
import Actor.TeamActor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.vecmath.Point2d;
import projectshanghai.World;

/**
 *
 * @author Eric-Standard
 */
public class Laser extends Projectile implements TeamActor {

    protected int side = 0;
    protected Point2d origin, endpoint;
    //Draw for .5 s
    private static final int drawTime = 100;
    protected int drawnTime = 0;
    public static final double DEFAULT_LENGTH = 1000;
    private double length;
    private boolean firstFrame = true;

    public Laser() {
        super();
    }

    public Laser(Actor owner, int damage) {
        super(owner);
        if (owner instanceof TeamActor) {
            TeamActor t = (TeamActor) owner;
            team = t.getTeam();
        }
        this.length = DEFAULT_LENGTH;
        this.angle = owner.getAngle();
        this.pos = (Point2d) owner.getPos().clone();
        this.endpoint = new Point2d();
        this.damage = damage;
        this.fuseTime = 1;
    }

    @Override
    public void act(long dt) {
        if (!firstFrame) {
            this.collisionDetection = false;
        }
        firstFrame = false;
        super.act(dt);
        drawnTime += dt;
        if (drawnTime > drawTime) {
            this.isActive = false;
            this.isDrawn = false;
        }
    }

    private void updateEndpoint() {
        endpoint.x = pos.x + length * Math.cos(angle);
        endpoint.y = pos.y + length * Math.sin(angle);
    }

    public void setLength(double length) {
        if (length >= 0) {
            this.length = length;
        }
        updateEndpoint();
    }

    @Override
    public void putSelfInWorld(World world) {
        if (world != null) {
            this.world = world;
            updateEndpoint();
            world.getProjectiles().add(this);
        }
    }

    @Override
    public void removeSelfFromWorld() {
        if (world != null) {
            world.getProjectiles().remove(this);
            this.world = null;
        }
    }

    @Override
    public void collide(Actor a) {
        //this.collisionDetection = false;
    }

    @Override
    public void draw(Graphics g) {
        if (endpoint == null) {
            //Draw to edge of screen
            double xAngle = Math.cos(angle);
            double yAngle = Math.sin(angle);
            //Let the Graphics do clipping
            //endpoint = new Point2d(pos.x + 1000 * xAngle, pos.y + 1000 * yAngle);
        }
        g.setColor(Color.red);
        g.drawLine((int) pos.x, (int) pos.y, (int) endpoint.x, (int) endpoint.y);
    }

    @Override
    public Rectangle2D getBoundingRectangle() {
        return null;
    }

    @Override
    public Shape getShape() {
        return new Line2D.Double(pos.x, pos.y, endpoint.x, endpoint.y);
    }

    @Override
    public int getTeam() {
        return team;
    }

    @Override
    public Actor getOwner() {
        return owner;
    }
}
